<?php
// No directAcces
defined( '_AppBackMiage' ) or die( 'Restricted access' );
if(isAdministrateur ()){
    include_once (DS."backoffice/include/header.php");
    include_once( DS."backoffice/include/toppanel_utilisateurs.php");
    ?>
    <div id="wrapper">
        <div id="content">
            <div id="rightnow">
                <?php
                $title = "Contacts Entreprises";
                $add = '<a class="add" href="'.URL.'index.php?m=admin_useradd_contact">Ajouter un contact entreprise</a>';
                ?>

                <h3 class="reallynow">
                    <span>Listes des <?php echo $title; ?></span>
                    <?php echo $add; ?>
                    <br />
                </h3>
                <p class="youhave">
                <form action="<?php echo URL; ?>index.php" method="get">
                    <input type="hidden" name="m" value="admin_usercontact_list"/>
                    <label for="name">Recherche : </label><input type="text" name="name" id="name" value="<?php echo $_GET['name']; ?>"/>
                    <label for="ent">Entreprise : </label>
                    <select name="ent">
                        <option value="0">-- Toutes --</option>
                        <?php
                        $lstEntreprise = Entreprise::getList();
                        if(!empty($lstEntreprise))
                        {
                            foreach($lstEntreprise as $v)
                            {
                                echo "<option value='" . $v->IdEntreprise . "'";
                                if (isset($_GET['ent']) && $_GET["ent"] == $v->IdEntreprise)
                                    echo " selected='selected' ";
                                echo ">" . stripslashes($v->NomEntreprise) . "</option>";
                            }
                        }
                        ?>
                    </select>
                    <input type="submit" value="Rechercher"/>
                </form>
                </p>
                <p class="youhave">
                <?php
                $nbrResultPerPage = 10;

                if (isset($_GET['page']) && !empty($_GET['page']))
                {
                    $page = $_GET['page'];
                    $nbr = (((int) $_GET['page'] - 1) * $nbrResultPerPage);
                    $lstUtilisateur = ContactEntreprise::getList($nbrResultPerPage, $nbr, $_GET['name'], $_GET['ent']);
                } else
                {
                    $page = 1;
                    $lstUtilisateur = ContactEntreprise::getList($nbrResultPerPage, 0, $_GET['name'], $_GET['ent']);
                }

                $count = ContactEntreprise::globalCount($_GET['name'], $_GET['ent']);
                $pages = ceil($count / $nbrResultPerPage);
                $pageSuivante = $page + 1;
                $pagePrecendente = $page - 1;
                if( !empty($lstUtilisateur))
                {
                ?>
                <table width="100%">
                    <thead>
                        <tr>
                            <th>Nom</th>
                            <th>Email</th>
                            <th>Date de derni&egrave;re visite</th>
                            <th>Entreprise</th>
                            <th width="60px">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach( $lstUtilisateur as $u){ ?>
                    <?php
                    $entreprise = new Entreprise($u->IdEntreprise);
                    $viewURL = URL."/index.php?m=admin_userview_contact&uid=".$u->IdUtilisateur;
                    $editURL = URL."/index.php?m=admin_useredit_contact&uid=".$u->IdUtilisateur;
                    ?>
                        <tr>
                            <td><?php echo $u->NomUtilisateur.' '.$u->PrenomUtilisateur; ?></td>
                            <td><?php echo $u->EmailUtilisateur; ?></td>
                            <td><?php echo date(DATE_FORMAT,$u->DateDerniereVisite); ?></td>
                            <td><?php echo $entreprise->getNomEntreprise(); ?></td>
                            <td>
                                <a href="<?php echo $viewURL; ?>">
                                    <img src="<?php echo STYLEBK; ?>img/icons/user.png" title="Show profile" width="16" height="16" />
                                </a>
                                <a href="<?php echo $editURL; ?>">
                                    <img src="<?php echo STYLEBK; ?>img/icons/user_edit.png" title="Edit user" width="16" height="16" />
                                </a>
                            </td>
                        </tr>
                    <?php } ?>
                    </tbody>
                </table>
                <div id="pager">
                    Page
                    <?php if($pagePrecendente > 0): ?>
                            <a href="<?php echo URL."/index.php?m=admin_usercontact_list&page=".$pagePrecendente; ?>&tid=2"><img src="<?php echo STYLEBK; ?>img/icons/arrow_left.gif" width="16" height="16" /></a>
                    <?php endif; ?>
                    <?php echo $page; ?>
                    <?php if($pageSuivante <= $pages): ?>
                            <a href="<?php echo URL."/index.php?m=admin_usercontact_list&page=".$pageSuivante; ?>&tid=2"><img src="<?php echo STYLEBK; ?>img/icons/arrow_right.gif" width="16" height="16" /></a>
                    <?php endif; ?>
                    sur <?php echo $pages; ?> | Total <strong><?php echo $count; ?></strong> utilisateurs trouves
                </div>
                <?php
                }
                else
                {
                        echo 'rien';
                }
                ?>
                </p>
            </div>
        </div>
    <?php
    include_once (DS."backoffice/include/sidebar.php");
    include_once (DS."backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>